package phoenix.workers.reports;

import core.config.Config;
import phoenix.core.PhoenixManager;
import phoenix.core.base.TestSuiteBase;

import java.util.List;

public class ReportCollector extends TestSuiteBase
{
	public static String p_message = "Redmine report is attached";

	public static void main(String args[])
	{

		if (phoenix == null)
		{
			phoenix = new PhoenixManager();
			// Use Build
			Config.BUILD = "live";
		}

		if (phoenix.config().REDMINE_POST_ERRORS)
		{
			try
			{

				List<String[]> issues = phoenix.redmine().reportCollector();
				String l_subject = "[Info][QA][" + phoenix.cronos().getCurrentDate() + "][Daily bugs]";

				if (issues.size() == 0)
				{
					// Do not send anything when no issues found
					phoenix.log().info("No issues have been found, quit");
					String messageContent = phoenix.html().addLinksCoverage();
					phoenix.file().write(phoenix.config().MAIL_HTML_PATH + phoenix.config().REPORT_FILE, messageContent);
					phoenix.mailExt()
							.sendRedmineDailyReport(phoenix.config().REPORT_TO, phoenix.config().REPORT_TO_CC, null, phoenix.config().REPORT_FROM, l_subject,
									"No issues have been found", phoenix.config().REPORT_FILE);
					return;
				}

				String messageContent = phoenix.html().buildReportTable(issues);
				phoenix.file().write(phoenix.config().MAIL_HTML_PATH + phoenix.config().REPORT_FILE, messageContent);
				phoenix.mailExt()
						.sendRedmineDailyReport(phoenix.config().REPORT_TO, phoenix.config().REPORT_TO_CC, null, phoenix.config().REPORT_FROM, l_subject,
								p_message,
								phoenix.config().REPORT_FILE);
				phoenix.log().info("ReportCollector has been executed");

			}
			catch (Exception p_ex)
			{
				phoenix.log().error("Something went wrong in report collector " + p_ex);
			}
		}
	}
}